<?php
//quando for local
if ($_SERVER['SERVER_NAME'] == "localhost") {
    include "../../rogerio/admin/config/define.php";
}else{
    include "../../admin/config/define.php";
}


set_include_path ($caminhoSite."/admin/scripts/".PATH_SEPARATOR.$caminhoKernel."/includes/".PATH_SEPARATOR.$caminhoKernel."/scripts/".PATH_SEPARATOR.$caminhoSite."/admin/");
	
	include_once("_onStartPage.php");
	include_once("_clsAllAction.php");
	
	$DEL = true;
        include_once($PATH_SITE."configField.inc.php");


	$objAllAction = new clsAllAction()
?>
<link rel="stylesheet" href="<?php echo PATH_CSS ?>admin.css" type="text/css">
<script language="JavaScript" src="<?= PATH_SCRIPTS?>jQuery/jquery-latest.js" type="text/javascript"></script>
<script language="JavaScript" src="<?= PATH_SCRIPTS?>jQuery/jquery.livequery.js" type="text/javascript"></script>
<?
	if( file_exists($PATH_SITE."include_jscript.php") ){
		include($PATH_SITE."include_jscript.php");
	}
?>
<script>

function showInsert(path){
	window.open(path,"","height=350,width=600,scrollbars=yes");
}

$(document).ready(function(){

	$(".delete_grid").click(function(){
	
		if(confirm("Tem certeza que deseja remover este(s) dado(s) ?")){
			$(this).parent().parent().remove();
			var  keyVal = $(this).parent().parent().attr("keyVal");
			$.ajax({
			type: "POST",
				url: "acoes_ajax.php",
				dataType: "html",
				data: "acao=3&keyName=<?=$KEY?>&keyValue="+keyVal+"&tableRel=<?=$TABLE?>",
				success: function( result ){
					alert(result);
				}
			});
		}else{
			return false;
		}
	});

});
</script>
<?
	if($ADD){
?>
<div style="cursor:pointer;width:80px;" onclick="showInsert('<?=$PATH_SITE?>adicionar.php?size=mini&keyValue=<?=$_GET['keyValue']?>&extraGrid=<?=urlencode(stripslashes($_GET['extraGrid']))?>')">
	<img alt="excluir" title="excluir" src="<?=PATH_IMGS?>add.gif" border="0" />Adicionar 
</div>
<?
	}
?>
<table width="60%" border="1" class="tabelaSimples">
	<tr>
<?
	if($DEL){
?>
		<td align="center" style="color:#996600">Excluir</td>
<?
	}
	
		for($j=0; $j< count($campos);$j++){
			if($campos[$j][1] != $KEY && $campos[$j][1] != "DT_CADASTRO" && $campos[$j][2] !=	"FK_HIDDEN" && $campos[$j][2] !=	"MASTER_DETAIL" ){
?>	
				<td align="center" style="color:#996600"><?=$campos[$j][0]?></td>
<?
			}
		}
		if( file_exists($PATH_SITE."include_header_list.php") ){
			include($PATH_SITE."include_header_list.php");
		}
?>			
	</tr>

<?
	$query = "	SELECT
					*
				FROM
					$TABLE
				WHERE ".base64_decode($_GET['keyName'])." = ".base64_decode($_GET['keyValue'])." ";
	if(isset($_GET['extraGrid'])){
		$extraGrid = unserialize(urldecode(stripslashes(base64_decode($_GET['extraGrid']))));
		if(is_array($extraGrid)){
			foreach ($extraGrid as $campoExtra=>$valorExtra) {
				$extraSql .= " AND ".$campoExtra." =".$valorExtra;
			}
			$query .= $extraSql;
		}
	}
	$query .= " ORDER BY ".$KEY." DESC ";
	$rs = $objDB->DBExecutarSQL( $query );
	$rsDados = $objDB->DBDadosRegistros($rs);
	
	foreach( $rsDados as $rsDado ){
?>
		<tr keyVal="<?=$rsDado->$KEY?>" <? if($EDIT){?> onclick="showInsert('<?=$PATH_SITE?>adicionar.php?size=mini&<?=$KEY?>=<?=$rsDado->$KEY?>&keyValue=<?=$_GET['keyValue']?>')" style="cursor:pointer" <?}?>>
<?
	if($DEL){
?>		
		<td width="5%" align="center">
			<img style="cursor:pointer" class="delete_grid" alt="excluir" title="excluir" src="<?=PATH_IMGS?>excluir.gif" border="0" />
		</td>
<?
	}
			for($j=0; $j< count($campos);$j++){
				if($campos[$j][1] != $KEY  && $campos[$j][1] != "DT_CADASTRO" && $campos[$j][2] !=	"FK_HIDDEN"){
					if($campos[$j][2] == "LOV2"){
						 $FIELD_WHERE_VAL = substr($campos[$j][20],strpos($campos[$j][20],".")+1,strlen($campos[$j][20]));
						 $rs = $objAllAction->getLov2( $campos[$j][7],$campos[$j][6],  $campos[$j][1], $campos[$j][20], $rsDado->$FIELD_WHERE_VAL );
						 $dados = $objDB->DBDadosRegistros($rs);
						 foreach ($dados as $dado) {
?>
							<td>
<?
								$k = 0;
								foreach ($campos[$j][7] as $FIELDS_SELECT) {
									$FIELD_ALIAS = $FIELDS_SELECT.$k;
									print $dado->$FIELD_ALIAS." ";
									$k++;	
								}
?>
							</td>					
<?
						 }
					}else if($campos[$j][2] == "LOV"){
						$rs = $objAllAction->getListaLov(  $campos[$j][1], $campos[$j][7],  $campos[$j][6] );
						$dados = $objDB->DBDadosRegistros($rs);
						foreach( $dados as $dado){
							if( $dado->$campos[$j][1] == $rsDado->$campos[$j][1]){
?>
								<td><?=$dado->$campos[$j][7]?></td>
<?
							}
						}
					}else if( $campos[$j][2] == "DATE_TIME"){
?>
						<td><?= date("d/m/Y",strtotime($rsDado->$campos[$j][1]))?></td>
						
<?					
					}else if( $campos[$j][2] == "TEXT"){
?>
						<td><?=$rsDado->$campos[$j][1]?></td>
<?					
					}else if($campos[$j][2] == "SELECT"){
						$SELECT_VALUES = $campos[$j][12];
						 foreach( $SELECT_VALUES as $key => $display ){
						 	if( $key == $rsDado->$campos[$j][1]){
?>
								<td><?=$display?></td>
<?							
							}
						 }
					}
				}
			}
			if( file_exists($PATH_SITE."include_value_list.php") ){
				include($PATH_SITE."include_value_list.php");
			}
?>
		<tr>
<?
	}
?>
</tr>	
</table>